// 引入 Dialog
import {
  checkAssetList,
  saveCheck,
  saveDetail
} from "@/api/check.js"
Page({
  data: {
    activeNames: [],
    checkGroups: {},
    isEmpty: true,
    show: false,
    discrepancyNote: "",
    inventoryCheckDetailId: "",
    checkId: "",
    assetId: "",
    difference: 0,
    actualQuantity: 1,
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onLoad(options) {
    this.getCheckList(options.id);
  },


  onClose(event) {
    this.setData({
      show: false
    })
  },


  //提交盘点
  onSaveCheck() {
    var data = []
    // 遍历每个 location
    for (let location in this.data.checkGroups) {
      const categories = this.data.checkGroups[location];

      // 遍历每个分类（如电脑、手机）
      for (let category in categories) {
        const assets = categories[category];

        // 遍历资产数组
        for (let i = 0; i < assets.length; i++) {
          var assetItem = assets[i]
          assetItem.id = assetItem.inventoryCheckDetailId
          data.push(assetItem)
        }
      }
    }

    saveCheck(data).then(res => {
      if (res.code == 200) {
        wx.showToast({
          title: '提交成功',
          mask: true
        })
      }
    })
  },
  //多选按钮切换
  onCheckChange(e) {
    const{categoryName,locationName,id}=e.currentTarget.dataset;
    const checkGroups=this.data.checkGroups
    for(let key in checkGroups){
      if(key===locationName){
        console.log(checkGroups[locationName][categoryName]);
        checkGroups[locationName][categoryName].map(item=>{
          if(item.id===id){
            item.checked=!item.checked;
            item.checked?item.actualQuantity=1:item.actualQuantity=0;
          }
        })
      }
    }
    this.setData({
      checkGroups
    })
    console.log(e);
  },
  onChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  },
//全选
checkCategoryAll(e){
  const{location,category}=e.currentTarget.dataset;
  const checkGroups=this.data.checkGroups
  for(let key in checkGroups){
    if(key===location){
      console.log(checkGroups[location][category]);
      checkGroups[location][category].map(item=>{
        item.checked=!item.checked
      })
    }
  }
  this.setData({
    checkGroups
  })
  console.log(e);
},
  //获取盘点详情数据
  async getCheckList(checkId) {
    this.data.checkGroups = []
    const result = await checkAssetList(checkId);
    var group = this.groupByLocationAndCategory(result.data);
    this.setData({
      checkGroups: group,
      isEmpty: Object.keys(group).length === 0
    });
  },
  //处理数据添加checked选中状态字段
  groupByLocationAndCategory(data) {
    const groupedByLocation = {};
    data.forEach(item => {
      const {
        locationName,
        categoryName
      } = item;
      if (!groupedByLocation[locationName]) {
        groupedByLocation[locationName] = {};
      }
      if (!groupedByLocation[locationName][categoryName]) {
        groupedByLocation[locationName][categoryName] = [];
      }
      if (item.actualQuantity == 1) {
        item.checked = true;
      } else {
        item.checked = false;
      }
      groupedByLocation[locationName][categoryName].push(item);
    });
    return groupedByLocation;
  },

  //差异说明按钮
  onAbnormalClick(event) {
    const dataset = event.currentTarget.dataset;
    this.setData({
      show: true,
      inventoryCheckDetailId: dataset.inventorycheckdetailid,
      checkId: dataset.checkid,
      assetId: dataset.assetsid
    })
  },
  //确定异常
  async confirm() {
    const result = await saveDetail({
      discrepancyNote: this.data.discrepancyNote,
      inventoryCheckDetailId: this.data.inventoryCheckDetailId,
      checkId: this.data.checkId,
      assetId: this.data.assetId,
      // difference: this.data.difference,
    })
    if (result.code === 200) {
      wx.showToast({
        title: '提交成功',
        icon: "error",
        mask: true
      })
    }
  },
});